package com.sjm.suchbear.minibookmall.action;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sjm.suchbear.minibookmall.model.BookModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Suchbear on 2018/5/5.
 */
public class BookManage {
    //获取图书列表
    public static List<BookModel> getBookList(SQLiteDatabase db) {
        List<BookModel> bookModels = new ArrayList<>();
        Cursor cursor = db.query("mall_book", new String[]{"bookID", "bookname", "bookauth", "bookcontent", "bookprice"}, null
                , null, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                BookModel bookModel = new BookModel();
                bookModel.setBookID(cursor.getString(0));
                bookModel.setBookname(cursor.getString(1));
                bookModel.setBookauth(cursor.getString(2));
                bookModel.setBookcontent(cursor.getString(3));
                bookModel.setBookprice(cursor.getString(4));
                bookModels.add(bookModel);
            }
            return bookModels;
        }
        cursor.close();
        return null;
    }

    //通过ID获取书籍信息
    public static BookModel getBookByID(SQLiteDatabase db, String bookID) {
        Cursor cursor = db.query("mall_book", new String[]{"bookID", "bookname", "bookauth", "bookcontent", "bookprice"}, "bookID=?"
                , new String[]{bookID}, null, null, null);
        if (cursor.moveToNext()) {
            BookModel bookModel = new BookModel();
            bookModel.setBookID(cursor.getString(0));
            bookModel.setBookname(cursor.getString(1));
            bookModel.setBookauth(cursor.getString(2));
            bookModel.setBookcontent(cursor.getString(3));
            bookModel.setBookprice(cursor.getString(4));
            return bookModel;
        }
        cursor.close();
        return null;
    }

    //新增书籍
    public static void addBook(SQLiteDatabase db, String bookname, String bookauth, String bookcontent, String bookprice) {
        ContentValues values = new ContentValues();
        values.put("bookname", bookname);
        values.put("bookauth", bookauth);
        values.put("bookcontent", bookcontent);
        values.put("bookprice", bookprice);
        db.insert("mall_book", null, values);
    }

    //修改书籍信息
    public static void changeBookInfo(SQLiteDatabase db, String bookID, String bookname, String bookauth, String bookcontent, String bookprice) {
        ContentValues values = new ContentValues();
        values.put("bookname", bookname);
        values.put("bookauth", bookauth);
        values.put("bookcontent", bookcontent);
        values.put("bookprice", bookprice);
        db.update("mall_book", values, "bookID=?", new String[]{bookID});
    }

    //根据ID删除图书
    public static void deleteBook(SQLiteDatabase db, String bookID) {
        db.delete("mall_book", "bookID=?", new String[]{bookID});
    }
}
